<?

/********************************************
* admin_rebuild_upflags.php
*
* This page iterates all matches, checks if they have video and flash up, and updates their flags accordingly.
*
********************************************/

//include files
include "../lib/db.php";
include "../lib/tbatv_lib.php";

$rootdir = "../tbatv/vids/"; //path to /vids/ folder.

//returns the disk path to a video.
function videopath($eventshort, $year, $complevel, $matchnumber, $extension) {
	//import global dictionaries
	global $complevelnames, $complevelshort, $weeknames;
	global $rootdir;

	//truncate year from YYYY to YY
	$yearshort = str_pad($year - 2000, 2, "0", STR_PAD_LEFT); //this will only work until 2100.
	
	//establish foldertree
	$dir = "{$yearshort}{$eventshort}/";
	
	//correct for database storage technique. Finals don't have the "1x" they should.
	if ($complevel == 50 AND $matchnumber < 10) $matchnumber+=10;
	
	//shift match number if its an elim.
	if ($complevel > 10) {
		$setnumber = floor($matchnumber / 10);
		$matchnumber = $matchnumber - $setnumber * 10;
	}
	
	//produce filename.
	$filename = "{$eventshort}_{$complevelshort[$complevel]}{$setnumber}m{$matchnumber}.{$extension}";
	
	//return path and name.
	return $rootdir . $dir . $filename;
}


function checkForVid($matchid, $ext, $path, $downgrade_on_failure=False) {
    global $count;
    if (file_exists($path)) {
		$query = mysql_query("UPDATE `matches` SET `vidup`=1, `vidext`='".$ext."' WHERE `matchid`='".$matchid."' LIMIT 1 ;");
		$count[$ext] += 1;
		verbose("<p>Match ".$matchid." established as ".$ext."</p>\n");
		return True;
	} elseif ($downgrade_on_failure) {
	    $query = mysql_query("UPDATE `matches` SET `vidup`=0, `vidext`='' WHERE `matchid`='".$matchid."' LIMIT 1 ;");
	    $count["viddowngraded"] += 1;
	    verbose("<p>Match ".$matchid." has no video file for ".$ext."</p>\n");
	}
	return False;
}

function checkForFlv($matchid, $path) {
    global $count;
    if (file_exists($path)) {
		$query = mysql_query("UPDATE `matches` SET `flvup`=1, WHERE `matchid`='".$matchid."' LIMIT 1 ;");
		$count["flv"] += 1;
		verbose("<p>Match ".$matchid." has flash video</p>\n");
		return True;
	} else {
	    $query = mysql_query("UPDATE `matches` SET `flvup`=0, WHERE `matchid`='".$matchid."' LIMIT 1 ;");
	    $count["flvdowngraded"] += 1;
	    verbose("<p>Match ".$matchid." has no flash video</p>\n");
	}
	return False;
}

function verbose($string) {
	if ($_REQUEST["verbose"] == 1) {
		echo $string;
	}
}

$start = microtime(true);

echo "<p>?verbose=1 for diagnostics.</p>";

// Hold the counts for each extension
$count = array();
$extensions = array("wmv", "avi", "m4v", "mp4", "xvid", "mov", "divx");

//look up all matches
$match_query_filter = "SELECT *, events.eventshort, events.year FROM `matches`, events WHERE events.eventid = matches.event";
$match_query = mysql_query($match_query_filter);

//Iterate through the matches and check to see if flags need updating
while($r=mysql_fetch_array($match_query)) {
	//matches seemingly lacking video
	if ($r["vidext"] == '' OR $r["vidext"] = ' ' OR $r["vidup"] == 0) { //check if an extension has been assigned.
	    $found_video = False;
		foreach ($extensions as $ext) {
		    $path = videopath($r["eventshort"], $r["year"], $r["complevel"], $r["matchnumber"], $ext);
		    if (checkForVid($r["matchid"], $ext, $path)) {
		        $found_video = True;
		        break; //great, bail on the extension loop.
		    }
		}
		if (!$found_video) {
		    //demote the video
		    checkForVid($r["matchid"], 'anything', $path, $downgrade_on_failure = True);
		}
	} else {
		//double check video file still exists
		$path = videopath($r["eventshort"], $r["year"], $r["complevel"], $r["matchnumber"], $r["vidext"]);
		checkForVid($r["matchid"], $r["vidext"], $downgrade_on_failure = True);
	}
	
	//all matches, check for flash
	if ($r["flvup"] == 0 OR $r["flvup"] == 1) {
	    $path = videopath($r["eventshort"], $r["year"], $r["complevel"], $r["matchnumber"], "flv");
		checkForFlv($r["matchid"], $path);
	} else {
		echo "Error on match {$r["matchid"]} flvup flag.<br />";
	}
}

foreach ($extensions as $ext) {
    echo "<p><strong>" . $count[$ext] . " " . $ext . " videos found.</strong></p>";
}
echo "<p><strong>" . $count["flv"] . " flash videos found.</strong></p>";
echo "<hr/>";
echo "<p><strong>" . $count["viddowngraded"] . " videos missing.</strong></p>";
echo "<p><strong>" . $count["flvdowngraded"] . " flash videos missing.</strong></p>";

$stop = microtime(true);
echo "Time elapsed: " . ($stop - $start) . " seconds";

?>


